package com.hjl.controller;

import com.alibaba.druid.util.StringUtils;
import com.hjl.pojo.User;
import com.hjl.service.UserService;
import com.hjl.utils.JwtHelper;
import com.hjl.utils.Result;
import com.hjl.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ServivE
 * @version 0.1
 * @className UserController
 * @description
 * @date 2024/8/22 16:07
 * @since jdk17
 */
@RestController
@RequestMapping("user")
@CrossOrigin//跨域注解
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtHelper jwtHelper;

    @PostMapping("login")
    public Result login(@RequestBody User user) {

        Result result = userService.login(user);
        System.out.println("result = " + result);
        return result;
    }

    //根据token获取用户数据
    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token) {
        Result result = userService.getUserInfo(token);
        return result;
    }

    //检查用户账号是否可用
    @PostMapping("checkUserName")
    public Result checkUserName(String username) {
        Result result = userService.checkUserName(username);
        return result;
    }

    //用户注册
    @PostMapping("regist")
    public Result regist(@RequestBody User user) {
        Result result = userService.regist(user);
        return result;
    }

    //登录检查
    @GetMapping("checkLogin")
    public Result checkLogin(@RequestHeader String token) {
        //判断token有效期
        boolean expiration = jwtHelper.isExpiration(token);
        if (StringUtils.isEmpty(token) || expiration) {
            //失效
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }
}
